package com.cloudkinto.controller.analysis;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.extentity.CustomerRecordsAnalysisDto;
import com.cloudkinto.extentity.StorageRecordsAnalysisDto;
import com.cloudkinto.extentity.orderinput.AccumulateOrderCount;
import com.cloudkinto.extentity.orderinput.AnalysisOrderCountDto;
import com.cloudkinto.extentity.output.OrderOutputSendCensusDto;
import com.cloudkinto.service.analysis.AnalysisService;
import com.cloudkinto.service.analysis.vo.AnalysisReq;
import com.cloudkinto.service.analysis.vo.CustomerOrderReq;
import com.cloudkinto.service.analysis.vo.MemberPageFeeRes;
import com.cloudkinto.service.analysis.vo.StockAgeReq;
import com.cloudkinto.service.covershows.vo.CustomerCaseAddReq;
import com.cloudkinto.service.stock.vo.log.StockLogPageRes;
import com.cloudkinto.service.storage.StorageService;
import io.swagger.annotations.*;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhujiale
 * @description
 * @date 2022/11/30
 */
@RestController
@Api(tags = "客户统计模块")
@RequestMapping("api/admin/analysis")
public class AnalysisController extends BaseController {
    @Autowired
    private AnalysisService service;
    @Autowired
    private StorageService storageService;

    @GetMapping("/init")
    @ApiOperation(value = "初始化", notes = "初始化")
    public Object init() {
        return service.init();
    }

    @GetMapping("/customerAnalysis")
    @ApiOperation(value = "客户统计", notes = "客户统计")
    public Object customerAnalysis() {
        return service.customerAnalysis();
    }

    @UserSessionRequired
    @PostMapping("/storageSituation")
    @ApiOperation(value = "仓库概况", notes = "仓库概况")
    public Object storageSituation(@RequestBody @Validated AnalysisReq req) {
        return service.storageSituation(req);
    }

    @UserSessionRequired
    @PostMapping("/stockAgeAnalysis")
    @ApiOperation(value = "库龄统计", notes = "库龄统计")
    public Object stockAgeAnalysis(@RequestBody @Validated StockAgeReq req) {
        return service.stockAgeAnalysis(req);
    }

    @UserSessionRequired
    @PostMapping("/orderAnalysis")
    @ApiOperation(value = "订单统计", notes = "订单统计")
    public Object orderAnalysis(@RequestBody @Validated AnalysisReq req) {
        return service.orderAnalysis(req);
    }

    @UserSessionRequired
    @PostMapping("/customerOrderAnalysis")
    @ApiOperation(value = "客户订单统计", notes = "客户订单统计")
    public PageResult customerOrderAnalysis(@RequestBody @Validated CustomerOrderReq req) {
        return service.customerOrderAnalysis(req);
    }

    @UserSessionRequired
    @PostMapping("/storageRecordsAnalysis")
    @ApiOperation(value = "仓库流水统计", notes = "仓库流水统计")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = StorageRecordsAnalysisDto.class)})
    public Object storageRecordsAnalysis(@RequestBody @Validated AnalysisReq req) {
        return service.storageRecordsAnalysis(req);
    }

    @UserSessionRequired
    @PostMapping("/customerRecordsAnalysis")
    @ApiOperation(value = "客户流水统计", notes = "客户流水统计")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = CustomerRecordsAnalysisDto.class)})
    public Object customerRecordsAnalysis(@RequestBody @Validated AnalysisReq req) {
        return service.customerRecordsAnalysis(req);
    }


    @UserSessionRequired
    @GetMapping("/censusSendCount")
    @ApiOperation(value = "统计一件代发订单数", notes = "统计一件代发订单数")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = OrderOutputSendCensusDto.class)})
    public Object censusSendCount(String startDate, String endDate, Long companyId) {
        return service.censusSendCount(startDate, endDate, companyId);
    }


    /**************************************会员首页********************************/

    @UserSessionRequired
    @GetMapping("/getToDealMsg")
    @ApiOperation(value = "新会员首页最上方统计", notes = "新会员首页最上方统计")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AnalysisOrderCountDto.class)})
    public SingleResult getToDealMsg(Long storageId) {
        List<AnalysisOrderCountDto> toDealMsg = service.getToDealMsg(storageId, getCurrentUserId());
        Map mapInfo = new HashMap();
        mapInfo.put("storageList", storageService.getStorageList());
        SingleResult singleResult = new SingleResult();
        singleResult.setData(toDealMsg);
        singleResult.setMapInfo(mapInfo);
        return singleResult;
    }

    @UserSessionRequired
    @PostMapping("/sortHeadLine")
    @ApiOperation(value = "新会员首页排序", notes = "新会员首页排序")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AnalysisOrderCountDto.class)})
    public SingleResult sortHeadLine(@RequestBody @Validated List<AnalysisOrderCountDto> list) {
        return SingleResult.success(service.sortHeadLine(list, getCurrentUserId()));
    }

    @UserSessionRequired
    @GetMapping("/accumulateOrderCount")
    @ApiOperation(value = "新会员首页 累计单量和产品库存", notes = "新会员首页 累计单量和产品库存")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AnalysisOrderCountDto.class)})
    public SingleResult accumulateOrderCount(Long storageId) {
        Map<String, Object> map = service.accumulateOrderCount(storageId);
        return SingleResult.success(map);
    }


    @UserSessionRequired
    @GetMapping("/orderCountAnalysis")
    @ApiOperation(value = "新会员首页 单量分析", notes = "新会员首页 单量分析")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AccumulateOrderCount.class)})
       @ApiImplicitParams({@ApiImplicitParam(name = "storageId", value = "仓库id"),
       @ApiImplicitParam(name = "type", value = "类型 input 入库单 output 一件代发 fba fba转运 back 退货单 repair 维修单"),
       @ApiImplicitParam(name = "beginTime", value = "开始时间 格式 yyyy-MM-dd"),
       @ApiImplicitParam(name = "endTime", value = "结束时间 格式 yyyy-MM-dd")})
    public SingleResult orderCountAnalysis(Long storageId, String type, String beginTime, String endTime) {
        List<AccumulateOrderCount> orderCountList = service.orderCountAnalysis(storageId, type, beginTime, endTime);
        return SingleResult.success(orderCountList);
    }


    @UserSessionRequired
    @GetMapping("/getFee")
    @ApiOperation(value = "新会员首页 费用", notes = "新会员首页 费用")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = MemberPageFeeRes.class)})
    @ApiImplicitParams({@ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间 格式 yyyy-MM-dd"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 格式 yyyy-MM-dd")})
    public SingleResult getFee(Long storageId, String beginTime, String endTime) {
        MemberPageFeeRes feeRes = service.getFee(storageId, beginTime, endTime);
        return SingleResult.success(feeRes);
    }
}
